package com.yeyks.app.controller.base.consumer;

import com.yeyks.app.annotation.UnUpdateCheck;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.utils.ArgumentsCheckUtil;
import com.yeyks.common.utils.TextValidator;
import com.yeyks.common.utils.Utility;
import com.yeyks.commonReference.service.consumer.CashOutOrderService;
import com.yeyks.commonReference.service.consumer.UserCouponInfoService;
import com.yeyks.consumer.dal.domain.UserInfo;
import com.yeyks.consumer.dto.BigGiftCouponListDTO;
import com.yeyks.consumer.dto.RecommendDetailDTO;
import com.yeyks.consumer.param.BagGiftPayParam;
import com.yeyks.consumer.service.RechargeRecordService;
import com.yeyks.consumer.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author fuchangshuai
 * @date 2019/4/25 0025
 */
@Api(tags = "推广接口", description = "推广")
@Slf4j
@RestController
@RequestMapping(value = "/app/recommend")
public class RecommendController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RechargeRecordService rechargeService;
    @Autowired
    private CashOutOrderService cashOutOrderService;
    @Autowired
    private UserCouponInfoService userCouponService;
    @Value("${recommend.price}")
    private Integer price;

    /**
     * 佣金明细
     *
     * @param token token
     * @param type  类型(21、活动通知下优惠券 22、活动通知下佣金  23、活动通知下活动 24、活动通知下钱包)
     * @return
     */
    @ApiOperation(value = "佣金明细v3.8", notes = "佣金明细", response = RecommendDetailDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型多个逗号分隔(22,活动通知下佣金,31订单佣金上级,32订单佣金上上级,41双旦红包)", defaultValue = "22", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "当前页", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "页码", defaultValue = "10", dataType = "int")
    })
    @RequestMapping(value = "/detail/list", method = RequestMethod.POST)
    public AjaxResponseBody<List<RecommendDetailDTO>> h5VipPay(@RequestParam(name = "token") String token,
                                                               @RequestParam(name = "type", defaultValue = "22") String type,
                                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        List<RecommendDetailDTO> recommendDetailDTOs = rechargeService.detailList(userInfo.getId(), type, pageNo, pageSize);
        return AjaxResponseBody.success(recommendDetailDTOs);
    }

    /**
     * 跳转我的佣金
     *
     * @param token
     * @return
     */
//    @ApiOperation(value = "跳转我的佣金", notes = "跳转我的佣金", response = AccountOverviewDTO.class)
//    @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String")
//    @PostMapping(value = "/info")
//    public AjaxResponseBody<AccountOverviewDTO> info(@RequestParam(name = "token") String token) {
//        UserInfo userInfo = userInfoService.tokenUserInfo(token);
//        AccountOverviewDTO accountOverviewDTO = cashOutOrderService.info(userInfo);
//        return AjaxResponseBody.success(accountOverviewDTO);
//    }

    /**
     * 推广规则
     *
     * @return
     */
    @ApiOperation(value = "推广规则", notes = "推广规则")
    @PostMapping(value = "/rule")
    public AjaxResponseBody<List<String>> rule() {
        List<String> lists = rechargeService.rule();
        return AjaxResponseBody.success(lists);
    }

    /**
     * 推广描述
     *
     * @return
     */
    @ApiOperation(value = "推广描述", notes = "推广描述图片")
    @PostMapping(value = "/desc")
    public AjaxResponseBody<Map<String, Object>> desc() {
        Map<String, Object> map = rechargeService.desc();
        return AjaxResponseBody.success(map);
    }

    /**
     * 推广活动详情
     *
     * @return
     */
    @ApiOperation(value = "推广活动详情", notes = "推广活动详情")
    @PostMapping(value = "/activity/detail")
    public AjaxResponseBody<Map<String, List<String>>> activityDetail() {
        Map<String, List<String>> maps = rechargeService.activityDetail();
        return AjaxResponseBody.success(maps);
    }

    /**
     * 获取活动链接
     *
     * @return
     */
    @ApiOperation(value = "获取活动链接", notes = "获取活动链接")
    @RequestMapping(value = "/url", method = RequestMethod.POST)
    @UnUpdateCheck
    public AjaxResponseBody url() {
        Object value = rechargeService.getUrl();
        return AjaxResponseBody.success(value);
    }

    /**
     * 判断用户是否已经充值
     *
     * @return
     */
    @ApiOperation(value = "判断用户是否已经充值", notes = "根据body中返回true(已充值)和false(未充值)")
    @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号", required = true, dataType = "String")
    @RequestMapping(value = "/is/recharge", method = RequestMethod.POST)
    public AjaxResponseBody<Boolean> isRecharge(@RequestParam(name = "phone") String phone) {
        checkParamPhone(phone);
        UserInfo userInfo = userInfoService.selectUseInfoPhone(phone);
        if (Objects.isNull(userInfo)) {
            return AjaxResponseBody.success(false);
        }
        return AjaxResponseBody.success(rechargeService.isRecharge(userInfo));
    }

    /**
     * 用户生成推广大礼包订单
     *
     * @param token
     * @param price
     * @return
     */
    @ApiOperation(value = "用户生成推广大礼包订单", notes = "用户生成推广大礼包订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "price", value = "充值金额", required = true, dataType = "int")
    })
    @PostMapping(value = "create/bag/gift/order")
    public AjaxResponseBody<Map<String, Object>> createBagGiftOrder(@RequestParam(name = "token") String token,
                                                                    @RequestParam(name = "inviteId", required = false) String inviteId,
                                                                    @RequestParam(name = "price") Integer price) {
        if (price == null || price < this.price) {
            throw new ServiceException("充值金额有误");
        }
        UserInfo loginUser = userInfoService.tokenUserInfo(token);
        if (rechargeService.isRecharge(loginUser)) {
            throw new ServiceException("您已参与过本次活动");
        }
        if (ArgumentsCheckUtil.notEmpty(inviteId)) {
            if (loginUser.getInviteCode() != null && inviteId.equals(loginUser.getInviteCode())) {
                throw new ServiceException("不能为本人邀请码");
            }
        }
        Map<String, Object> map = rechargeService.createBagGiftOrder(price, loginUser, inviteId);
        return AjaxResponseBody.success(map);
    }

    /**
     * 推广大礼包充值
     *
     * @return
     */
    @ApiOperation(value = "推广大礼包充值", notes = "推广大礼包充值")
    @RequestMapping(value = "/bag/gift/pay", method = RequestMethod.POST)
    public AjaxResponseBody<Object> bagGiftPay(@Valid BagGiftPayParam bagGiftPayParam, HttpServletRequest request) {
        UserInfo userInfo = userInfoService.tokenUserInfo(bagGiftPayParam.getToken());
        if (rechargeService.isRecharge(userInfo)) {
            throw new ServiceException("您已参与过本次活动");
        }
        return AjaxResponseBody.success(rechargeService.bagGiftPay(userInfo, bagGiftPayParam, Utility.getRemoteIP(request)));
    }

    private void checkParamPhone(String phone) {
        if (ArgumentsCheckUtil.isEmpty(phone)) {
            throw new ServiceException("手机号不能为空");
        }
        if (!TextValidator.MOBILE.boolCheck(phone)) {
            throw new ServiceException("手机号格式非法");
        }
    }

    /**
     * 推广大礼包查询
     *
     * @return
     */
    @ApiOperation(value = "推广大礼包查询", notes = "推广大礼包查询")
    @RequestMapping(value = "/bag/gift/couponList", method = RequestMethod.POST)
    public AjaxResponseBody<List<BigGiftCouponListDTO>> bagGiftCouponList(@RequestParam(name = "token") String token,
                                                                          @RequestParam(name = "orderNo") String orderNo) {
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        List<BigGiftCouponListDTO> userCouponInfos = userCouponService.bagGiftCouponList(userInfo.getId(), orderNo);
        if (userCouponInfos == null || userCouponInfos.size() == 0) {
            return AjaxResponseBody.success();
        }
        return AjaxResponseBody.success(userCouponInfos);
    }

}
